package com.mingtu.jgspatrol.fragment;

import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.DeviceUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.interfaces.OnCancelListener;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.mingtu.common.base.BaseBindingFragment;
import com.mingtu.common.callback.StringDialogCallback;
import com.mingtu.common.utils.Constant;
import com.mingtu.common.utils.GridSpacingItemDecoration;
import com.mingtu.common.viewpager.LazyViewPager.LazyFragmentPagerAdapter;
import com.mingtu.jgspatrol.R;
import com.mingtu.jgspatrol.adapter.ScreenControlAdapter2;
import com.mingtu.jgspatrol.databinding.FragmentScreenControlBinding;
import com.mingtu.jgspatrol.utils.MyConstant;

import java.util.HashMap;

public class ScreenControlFragment2 extends BaseBindingFragment<FragmentScreenControlBinding> implements LazyFragmentPagerAdapter.Laziable {
    private ScreenControlAdapter2 screenControlAdapter2;
    private String[] valueArray = {
            "pm", "pm10",
            "fylz", "yl",
            "fs", "fx",
            "kqwd1", "dqy",
            "jfs", "zfs",
            "kqsd1","szph1",
            "yw","zd",
            "szddl","rjy",
            "swd"
    };
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_screen_control;
    }

    @Override
    protected void initData() {
        boolean isTable = DeviceUtils.isTablet();
        int spanCount = isTable ? 3 : 2;
        GridLayoutManager layoutManager = new GridLayoutManager(context, spanCount);//第二个参数为网格的列数
        binding.recycler.addItemDecoration(new GridSpacingItemDecoration(spanCount, SizeUtils.dp2px(15), true));
        binding.recycler.setLayoutManager(layoutManager);

        screenControlAdapter2 = new ScreenControlAdapter2(context);
        binding.recycler.setAdapter(screenControlAdapter2);
        screenControlAdapter2.setOnItemClickListener(new ScreenControlAdapter2.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                screenControlAdapter2.upData(position);
                new XPopup.Builder(getContext())
                        .isDestroyOnDismiss(false)
                        .dismissOnTouchOutside(true)
                        .hasShadowBg(true)
                        .asConfirm(context.getResources().getString(R.string.text_tip), "是否发出控制命令？",
                                context.getResources().getString(R.string.text_cancle), context.getResources().getString(R.string.text_sure),
                                new OnConfirmListener() {
                                    @Override
                                    public void onConfirm() {
                                        HashMap<String, Object> hashMap = new HashMap<>();
                                        hashMap.put("type", 2);
                                        hashMap.put("value", valueArray[position]);
                                        String paramStr = GsonUtils.toJson(hashMap);
                                        outdoorMsgSend(paramStr);
                                    }
                                }, new OnCancelListener() {
                                    @Override
                                    public void onCancel() {
                                        screenControlAdapter2.resetData();
                                    }
                                }, false)
                        .show();
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void getData() {

    }

    /**
     * 发送大屏控制
     */
    private void outdoorMsgSend(String paramStr) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("message", paramStr);
        String str = GsonUtils.toJson(hashMap);
        OkGo.<String>post(MyConstant.POST_OUTDOOR_SEND)
                .tag(this)
                .upJson(str)
                .execute(new StringDialogCallback(getActivity()) {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String result = response.body();
                        if (!StringUtils.isEmpty(result)) {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String msg = jsonObject.getString("msg");
                            if (msg.equals(Constant.RESULT_SUCCESS)) {
                                ToastUtils.showLong("控制命令发送成功！");
                            } else {
                                ToastUtils.showLong("控制命令发送失败！");
                            }
                        }
                    }
                });
    }


}